package com.ebei.admin.common.util;

import com.ebei.common.constant.CommonConstant;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;


public final class CommonUtil {

    public static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    public static void setAlive(Map<String, Object> map) {
        map.put("delFlag", CommonConstant.STATUS_NORMAL);
    }

    public static void setTop(Map<String, Object> map) {
        map.put("parentId", -1);
    }

    public static void setCreateTime(Map<String, Object> map) {
        map.put("createTime", new Date());
    }

    public static boolean isUnder(String boss, String unit) {
        //字节点的层级id  以父节点的层级id开头,,,而且子节点的长度大于父节点的长度
        if (unit.startsWith(boss) && unit.length() > boss.length()) {
            return true;
        }
        return false;
    }

    public static String getAncestor(String son) {
        if (StringUtils.hasLength(son)) {
            //截取前四个,,0,1,2,3 含头不含尾
            return son.substring(0, 4);
        }
        return null;
    }

    public static String randomString(int i) {
        return RandomStringUtils.random(i, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
    }

    public static String getLevelTop(String level) {
        int mask = CommonConstant.ORGTYPE_MAX_SIZE.length();
        if (level != null && level.length() > mask) {
            return level.substring(0, mask - 1);
        }
        return level;
    }

    public static Map<String, Object> removeT(Map<String, Object> map) {
        if (map.containsKey("_t")) {
            map.remove("_t");
            return map;
        }
        return map;
    }

}
